﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.FileManipulation
{
    /// <summary></summary>
    public class CompressionWorker:IWorker
    {
        private Thread _WorkerThread;
        private CompressionInstruction _Instruction;
        private CompressionOutput _Output;
        private RunContext _Context;
        private Guid _TicketID;
        private string _WorkflowName;
        private string _ComponentName;

        /// <summary>
        /// 
        /// </summary>
        public string ComponentName
        {
            get { return _ComponentName; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string WorkflowName
        {
            get { return _WorkflowName; }
        }

        /// <summary></summary>
        public Thread WorkerThread
        {
            get { return _WorkerThread; }
        }
        /// <summary></summary>
        public InstructionBase Instruction
        {
            get { return _Instruction; }
        }
        /// <summary></summary>
        public ResultBase Output
        {
            get { return _Output; }
        }
        /// <summary></summary>
        public Type ComponentType
        {
            get { return typeof(CompressionComponent); }
        }
        /// <summary></summary>
        public RunContext Context
        {
            get { return _Context; }
            set { _Context = value; }
        }
        /// <summary></summary>
        public void SetInstruction(Guid ticketGuid, InstructionBase request, string workflowName, string componentName)
        {
            this._TicketID = ticketGuid;
            this._Instruction = (CompressionInstruction) request;
            this._WorkflowName = workflowName;
            this._ComponentName = componentName;
        }
        /// <summary></summary>
        public void SetInstruction(RunContext context, Guid ticketGuid, InstructionBase request, string workflowName, string componentName)
        {
            this._TicketID = ticketGuid;
            this._Instruction = (CompressionInstruction)request;
            this._Context = context;
            this._WorkflowName = workflowName;
            this._ComponentName = componentName;
        }
        /// <summary></summary>
        public void DoWork()
        {
            if(this.WorkerStartedEvent !=null)
            {
                this.WorkerStartedEvent(this._TicketID, this, this.GetType(), this._Instruction);
            }

            this._WorkerThread = Thread.CurrentThread;
            if(this._Instruction.Action==CompressionActionType.WinzipCompress)
            {
                this.DoWinzipCompression();
            }
            else if(this._Instruction.Action==CompressionActionType.WinzipDecompress)
            {
                this.DoWinzipUncompression();
            }
            else
            {
                if (this.WorkerErredEvent != null)
                {
                    this.WorkerErredEvent(this._TicketID, this.GetType(), this._Instruction, "Not implemented",true);
                }
            }
        }
        /// <summary></summary>
        public void DoWorkAsync()
        {
            if (this.WorkerStartedEvent != null)
            {
                this.WorkerStartedEvent(this._TicketID, this, this.GetType(), this._Instruction);
            }

            
            if (this._Instruction.Action == CompressionActionType.WinzipCompress)
            {
                this._WorkerThread = new Thread(new ThreadStart(this.DoWinzipCompression));
                this._WorkerThread.Start();
            }
            else if (this._Instruction.Action == CompressionActionType.WinzipDecompress)
            {
                this._WorkerThread = new Thread(new ThreadStart(this.DoWinzipUncompression));
                this._WorkerThread.Start();
            }
            else
            {
                if(this.WorkerErredEvent !=null)
                {
                    this.WorkerErredEvent(this._TicketID, this.GetType(), this._Instruction, "Not implemented",true);
                }
            }
            
        }
        /// <summary></summary>
        public void CancelWork()
        {
            throw new NotImplementedException();
        }

        public event WorkerStarted WorkerStartedEvent;
        public event WorkerFinished WorkerFinishedEvent;
        public event WorkerHasError WorkerErredEvent;
        public event WorkerCancelled WorkerCancelledEvent;
        public event WorkerStatusChanged WorkerStatusChangedEvent;

        #region work
        private void DoWinzipCompression()
        {
            try
            {
                WinzipWrapper.ZipFile(this._Instruction.WinzipProgramFilePath, this._Instruction.SrcPath,
                                      this._Instruction.TgtPath, this._Instruction.Password);
                this._Output =
                    new CompressionOutput(this._Instruction.OwnerComponentID, this._TicketID, this._Instruction.TgtPath);
                if(this.WorkerFinishedEvent !=null)
                {
                    this.WorkerFinishedEvent(this._TicketID, this.GetType(), this._Instruction, this._Output);
                }
            }
            catch(Exception ex)
            {
                if(this.WorkerErredEvent !=null)
                {
                    this.WorkerErredEvent(this._TicketID, this.GetType(), this._Instruction, ex.ToString(),true);
                }
            }
        }

        private void DoWinzipUncompression()
        {
            try
            {
                WinzipWrapper.UnzipFile(this._Instruction.WinzipProgramFilePath, this._Instruction.SrcPath,
                                      this._Instruction.TgtPath, this._Instruction.Password);
                this._Output =
                    new CompressionOutput(this._Instruction.OwnerComponentID, this._TicketID, this._Instruction.TgtPath);
                if (this.WorkerFinishedEvent != null)
                {
                    this.WorkerFinishedEvent(this._TicketID, this.GetType(), this._Instruction, this._Output);
                }
            }
            catch (Exception ex)
            {
                if (this.WorkerErredEvent != null)
                {
                    this.WorkerErredEvent(this._TicketID, this.GetType(), this._Instruction, ex.ToString(),true);
                }
            }
        }
        #endregion
    }
}
